%% On the Optimal Design of Transfers and Income-Tax Progressivity
% Tax-and-transfer rates plots from quantitative model output

clear; clc; close all;
addpath('../../../../Matlab_Figure_Files');

%% Read results

% Number of grid points
n = 1000;

% Labor income grid
yl_grid = readmatrix("yl_grid.txt");
yl_grid = yl_grid';
yl_grid = yl_grid(1:n)';

% Initial steady state; zero capital income

% Average tax-and-transfer rates initial steady state (zero capital income)
avg_tax_transfer_zerok_init = readmatrix("avg_tax_transfer_zerok_init.txt");
avg_tax_transfer_zerok_init = avg_tax_transfer_zerok_init';
avg_tax_transfer_zerok_init = avg_tax_transfer_zerok_init(1:n)';

% Marginal tax-and-transfer rates initial steady state (zero capital income)
marg_tax_transfer_zerok_init = readmatrix("marg_tax_transfer_zerok_init.txt");
marg_tax_transfer_zerok_init = marg_tax_transfer_zerok_init';
marg_tax_transfer_zerok_init = marg_tax_transfer_zerok_init(1:n)';

% Average tax rates initial steady state (zero capital income)
avg_tax_zerok_init = readmatrix("avg_tax_zerok_init.txt");
avg_tax_zerok_init = avg_tax_zerok_init';
avg_tax_zerok_init = avg_tax_zerok_init(1:n)';

% Marginal tax rates initial steady state (zero capital income)
marg_tax_zerok_init = readmatrix("marg_tax_zerok_init.txt");
marg_tax_zerok_init = marg_tax_zerok_init';
marg_tax_zerok_init = marg_tax_zerok_init(1:n)';

% Average transfer rates initial steady state (zero capital income)
avg_transfer_zerok_init = readmatrix("avg_transfer_zerok_init.txt");
avg_transfer_zerok_init = avg_transfer_zerok_init';
avg_transfer_zerok_init = avg_transfer_zerok_init(1:n)';

% Marginal transfer rates initial steady state (zero capital income)
marg_transfer_zerok_init = readmatrix("marg_transfer_zerok_init.txt");
marg_transfer_zerok_init = marg_transfer_zerok_init';
marg_transfer_zerok_init = marg_transfer_zerok_init(1:n)';

% Final steady state; zero capital income

% Average tax-and-transfer rates final steady state (zero capital income)
avg_tax_transfer_zerok_lr = readmatrix("avg_tax_transfer_zerok_lr.txt");
avg_tax_transfer_zerok_lr = avg_tax_transfer_zerok_lr';
avg_tax_transfer_zerok_lr = avg_tax_transfer_zerok_lr(1:n)';

% Marginal tax-and-transfer rates final steady state (zero capital income)
marg_tax_transfer_zerok_lr = readmatrix("marg_tax_transfer_zerok_lr.txt");
marg_tax_transfer_zerok_lr = marg_tax_transfer_zerok_lr';
marg_tax_transfer_zerok_lr = marg_tax_transfer_zerok_lr(1:n)';

% Average tax rates final steady state (zero capital income)
avg_tax_zerok_lr = readmatrix("avg_tax_zerok_lr.txt");
avg_tax_zerok_lr = avg_tax_zerok_lr';
avg_tax_zerok_lr = avg_tax_zerok_lr(1:n)';

% Marginal tax rates final steady state (zero capital income)
marg_tax_zerok_lr = readmatrix("marg_tax_zerok_lr.txt");
marg_tax_zerok_lr = marg_tax_zerok_lr';
marg_tax_zerok_lr = marg_tax_zerok_lr(1:n)';

% Average transfer rates final steady state (zero capital income)
avg_transfer_zerok_lr = readmatrix("avg_transfer_zerok_lr.txt");
avg_transfer_zerok_lr = avg_transfer_zerok_lr';
avg_transfer_zerok_lr = avg_transfer_zerok_lr(1:n)';

% Marginal transfer rates final steady state (zero capital income)
marg_transfer_zerok_lr = readmatrix("marg_transfer_zerok_lr.txt");
marg_transfer_zerok_lr = marg_transfer_zerok_lr';
marg_transfer_zerok_lr = marg_transfer_zerok_lr(1:n)';

% Initial steady state; mean capital income

% Average tax-and-transfer rates initial steady state (mean capital income)
avg_tax_transfer_meank_init = readmatrix("avg_tax_transfer_meank_init.txt");
avg_tax_transfer_meank_init = avg_tax_transfer_meank_init';
avg_tax_transfer_meank_init = avg_tax_transfer_meank_init(1:n)';

% Marginal tax-and-transfer rates initial steady state (mean capital income)
marg_tax_transfer_meank_init = readmatrix("marg_tax_transfer_meank_init.txt");
marg_tax_transfer_meank_init = marg_tax_transfer_meank_init';
marg_tax_transfer_meank_init = marg_tax_transfer_meank_init(1:n)';

% Average tax rates initial steady state (mean capital income)
avg_tax_meank_init = readmatrix("avg_tax_meank_init.txt");
avg_tax_meank_init = avg_tax_meank_init';
avg_tax_meank_init = avg_tax_meank_init(1:n)';

% Marginal tax rates initial steady state (mean capital income)
marg_tax_meank_init = readmatrix("marg_tax_meank_init.txt");
marg_tax_meank_init = marg_tax_meank_init';
marg_tax_meank_init = marg_tax_meank_init(1:n)';

% Average transfer rates initial steady state (mean capital income)
avg_transfer_meank_init = readmatrix("avg_transfer_meank_init.txt");
avg_transfer_meank_init = avg_transfer_meank_init';
avg_transfer_meank_init = avg_transfer_meank_init(1:n)';

% Marginal transfer rates initial steady state (mean capital income)
marg_transfer_meank_init = readmatrix("marg_transfer_meank_init.txt");
marg_transfer_meank_init = marg_transfer_meank_init';
marg_transfer_meank_init = marg_transfer_meank_init(1:n)';

% Final steady state; mean capital income

% Average tax-and-transfer rates final steady state (mean capital income)
avg_tax_transfer_meank_lr = readmatrix("avg_tax_transfer_meank_lr.txt");
avg_tax_transfer_meank_lr = avg_tax_transfer_meank_lr';
avg_tax_transfer_meank_lr = avg_tax_transfer_meank_lr(1:n)';

% Marginal tax-and-transfer rates final steady state (mean capital income)
marg_tax_transfer_meank_lr = readmatrix("marg_tax_transfer_meank_lr.txt");
marg_tax_transfer_meank_lr = marg_tax_transfer_meank_lr';
marg_tax_transfer_meank_lr = marg_tax_transfer_meank_lr(1:n)';

% Average tax rates final steady state (mean capital income)
avg_tax_meank_lr = readmatrix("avg_tax_meank_lr.txt");
avg_tax_meank_lr = avg_tax_meank_lr';
avg_tax_meank_lr = avg_tax_meank_lr(1:n)';

% Marginal tax rates final steady state (mean capital income)
marg_tax_meank_lr = readmatrix("marg_tax_meank_lr.txt");
marg_tax_meank_lr = marg_tax_meank_lr';
marg_tax_meank_lr = marg_tax_meank_lr(1:n)';

% Average transfer rates final steady state (mean capital income)
avg_transfer_meank_lr = readmatrix("avg_transfer_meank_lr.txt");
avg_transfer_meank_lr = avg_transfer_meank_lr';
avg_transfer_meank_lr = avg_transfer_meank_lr(1:n)';

% Marginal transfer rates final steady state (mean capital income)
marg_transfer_meank_lr = readmatrix("marg_transfer_meank_lr.txt");
marg_transfer_meank_lr = marg_transfer_meank_lr';
marg_transfer_meank_lr = marg_transfer_meank_lr(1:n)';

%% Plot initial steady state

fig = figure(101);

subplot(1,2,1)
plot(yl_grid,avg_transfer_meank_init,':','Color',[0.96500, 0.6550, 0.4480],'linewidth',2);  hold on
plot(yl_grid,avg_tax_meank_init+avg_transfer_meank_init,'Color',[0.9590, 0.9140, 0.5450],'linewidth',2); hold on
h1 = plot(yl_grid,avg_tax_zerok_init,':','Color',[0, 0.4470, 0.7410],'linewidth',2); hold on
h2 = plot(yl_grid,avg_transfer_zerok_init,':','Color',[0.8500, 0.3250, 0.0980],'linewidth',2); hold on
h3 = plot(yl_grid,avg_tax_zerok_init+avg_transfer_zerok_init,'Color',[0.9290, 0.6940, 0.1250],'linewidth',2);
xlim([0 5.0])
leg = legend([h1 h2 h3],{'Tax','Transfer','Total $t\&T$ rate'});
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12)                
legend boxoff
ylabel('Average rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y_\ell$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12)
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.6  0.8])

subplot(1,2,2)
plot(yl_grid,marg_transfer_meank_init,':','Color',[0.96500, 0.6550, 0.4480],'linewidth',2); hold on
plot(yl_grid,marg_tax_meank_init+marg_transfer_meank_init,'Color',[0.9590, 0.9140, 0.5450],'linewidth',2);  hold on
h1 = plot(yl_grid,marg_tax_zerok_init,':','Color',[0, 0.4470, 0.7410],'linewidth',2); hold on
h2 = plot(yl_grid,marg_transfer_zerok_init,':','Color',[0.8500, 0.3250, 0.0980],'linewidth',2); hold on
h3 = plot(yl_grid,marg_tax_zerok_init+marg_transfer_zerok_init,'Color',[0.9290, 0.6940, 0.1250],'linewidth',2); 
xlim([0 5.0])
ylabel('Marginal rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y_\ell$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.6  0.8])
run graph_extended.m
saveas(gcf,'qm_implied_rates_init.pdf')


fig = figure(102);

subplot(1,2,1)
plot(yl_grid,avg_transfer_meank_init,':','Color',[0.5 0.5 0.5],'linewidth',2);  hold on
plot(yl_grid,avg_tax_meank_init+avg_transfer_meank_init,'Color',[0.5 0.5 0.5],'linewidth',2); hold on
h1 = plot(yl_grid,avg_tax_zerok_init,'--','Color',[0 0 0],'linewidth',2); hold on
h2 = plot(yl_grid,avg_transfer_zerok_init,':','Color',[0 0 0],'linewidth',2); hold on
h3 = plot(yl_grid,avg_tax_zerok_init+avg_transfer_zerok_init,'Color',[0 0 0],'linewidth',2);
xlim([0 5.0])
leg = legend([h1 h2 h3],{'Tax','Transfer','Total $t\&T$ rate'});
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12)                
legend boxoff
ylabel('Average rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y_\ell$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12)
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.6  0.8])

subplot(1,2,2)
plot(yl_grid,marg_transfer_meank_init,':','Color',[0.5 0.5 0.5],'linewidth',2); hold on
plot(yl_grid,marg_tax_meank_init+marg_transfer_meank_init,'Color',[0.5 0.5 0.5],'linewidth',2);  hold on
h1 = plot(yl_grid,marg_tax_zerok_init,'--','Color',[0 0 0],'linewidth',2); hold on
h2 = plot(yl_grid,marg_transfer_zerok_init,':','Color',[0 0 0],'linewidth',2); hold on
h3 = plot(yl_grid,marg_tax_zerok_init+marg_transfer_zerok_init,'Color',[0 0 0],'linewidth',2); 
xlim([0 5.0])
ylabel('Marginal rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y_\ell$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.6  0.8])
run graph_extended.m
saveas(gcf,'qm_implied_rates_init_bw.pdf')
saveas(gcf,'qm_implied_rates_init_bw','epsc')

%% Plot final steady state

fig = figure(103);
subplot(1,2,1)
plot(yl_grid,avg_transfer_meank_lr,':','Color',[0.96500, 0.6550, 0.4480],'linewidth',2);  hold on
plot(yl_grid,avg_tax_meank_lr+avg_transfer_meank_lr,'Color',[0.9590, 0.9140, 0.5450],'linewidth',2); hold on
h1 = plot(yl_grid,avg_tax_zerok_lr,':','Color',[0, 0.4470, 0.7410],'linewidth',2); hold on
h2 = plot(yl_grid,avg_transfer_zerok_lr,':','Color',[0.8500, 0.3250, 0.0980],'linewidth',2); hold on
h3 = plot(yl_grid,avg_tax_zerok_lr+avg_transfer_zerok_lr,'Color',[0.9290, 0.6940, 0.1250],'linewidth',2);
xlim([0 5.0])
leg = legend([h1 h2 h3],{'Tax','Transfer','Total $t\&T$ rate'});
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12)                
legend boxoff
ylabel('Average rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y_\ell$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12)
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.6  0.8])

subplot(1,2,2)
plot(yl_grid,marg_transfer_meank_lr,':','Color',[0.96500, 0.6550, 0.4480],'linewidth',2); hold on
plot(yl_grid,marg_tax_meank_lr+marg_transfer_meank_lr,'Color',[0.9590, 0.9140, 0.5450],'linewidth',2);  hold on
h1 = plot(yl_grid,marg_tax_zerok_lr,':','Color',[0, 0.4470, 0.7410],'linewidth',2); hold on
h2 = plot(yl_grid,marg_transfer_zerok_lr,':','Color',[0.8500, 0.3250, 0.0980],'linewidth',2); hold on
h3 = plot(yl_grid,marg_tax_zerok_lr+marg_transfer_zerok_lr,'Color',[0.9290, 0.6940, 0.1250],'linewidth',2); 
xlim([0 5.0])
ylabel('Marginal rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y_\ell$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.6  0.8])
run graph_extended.m
saveas(gcf,'qm_implied_rates_lr.pdf')

fig = figure(104);
subplot(1,2,1)
plot(yl_grid,avg_transfer_meank_lr,':','Color',[0.5 0.5 0.5],'linewidth',2);  hold on
plot(yl_grid,avg_tax_meank_lr+avg_transfer_meank_lr,'Color',[0.5 0.5 0.5],'linewidth',2); hold on
h1 = plot(yl_grid,avg_tax_zerok_lr,'--','Color',[0 0 0],'linewidth',2); hold on
h2 = plot(yl_grid,avg_transfer_zerok_lr,':','Color',[0 0 0],'linewidth',2); hold on
h3 = plot(yl_grid,avg_tax_zerok_lr+avg_transfer_zerok_lr,'Color',[0 0 0],'linewidth',2);
xlim([0 5.0])
leg = legend([h1 h2 h3],{'Tax','Transfer','Total $t\&T$ rate'});
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12)                
legend boxoff
ylabel('Average rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y_\ell$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12)
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.6  0.8])

subplot(1,2,2)
plot(yl_grid,marg_transfer_meank_lr,':','Color',[0.5 0.5 0.5],'linewidth',2); hold on
plot(yl_grid,marg_tax_meank_lr+marg_transfer_meank_lr,'Color',[0.5 0.5 0.5],'linewidth',2);  hold on
h1 = plot(yl_grid,marg_tax_zerok_lr,'--','Color',[0 0 0],'linewidth',2); hold on
h2 = plot(yl_grid,marg_transfer_zerok_lr,':','Color',[0 0 0],'linewidth',2); hold on
h3 = plot(yl_grid,marg_tax_zerok_lr+marg_transfer_zerok_lr,'Color',[0 0 0],'linewidth',2); 
xlim([0 5.0])
ylabel('Marginal rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y_\ell$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.6  0.8])
run graph_extended.m
saveas(gcf,'qm_implied_rates_lr_bw.pdf')
saveas(gcf,'qm_implied_rates_lr_bw','epsc')






